import modelExtend from 'dva-model-extend'
import { detail } from './services/financialReceipt'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';
import pathToRegexp from 'path-to-regexp';

export default modelExtend(lwjPageModel, {
  namespace: 'receiptDetail',
  state: {
    currentItemDetail: {},
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        const match = pathToRegexp('/receipt/detail/:id').exec(pathname);
        if (match) {
          dispatch({
            type: 'detailSuccess', payload: {
              currentItemDetail: {}
            }
          })
          const id = match[1];
          dispatch({
            type: 'detail',
            payload: {
              id: id
            }
          })
        }
      })
    }
  },
  effects: {
    * detail({
      payload
    }, { select, call, put }) {
      const data = yield call(detail, payload.id);
      if (data.result)
        yield put({
          type: 'detailSuccess', payload: {
            currentItemDetail: data.result,
          }
        })
    }
  },
  reducers: {
    detailSuccess(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    }
  }
})
